package com.basicalg.basicalgorithms;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;

public class LayoutVHApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        VBox vBox = new VBox();

        HBox topControls = new HBox();
        VBox.setMargin(topControls, new Insets(10.0d));
        topControls.setAlignment(Pos.BOTTOM_LEFT);

        Button btnRefresh = new Button("Refresh");
        HBox topRightControls = new HBox();
        HBox.setHgrow(topRightControls, Priority.ALWAYS);
        topRightControls.setAlignment(Pos.BOTTOM_RIGHT);
        Hyperlink signOutLink = new Hyperlink("Sign Out");
        topRightControls.getChildren().add(signOutLink);

        topControls.getChildren().addAll(btnRefresh, topRightControls);

        TableView<Customer> tblCustomer = new TableView<>();
        tblCustomer.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        VBox.setMargin(tblCustomer, new Insets(0.0d, 10.0d, 10.0d,10.0d));
        VBox.setVgrow(tblCustomer, Priority.ALWAYS);

        TableColumn<Customer, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));

        TableColumn<Customer, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        tblCustomer.getColumns().addAll(lastNameCol, firstNameCol);

        Separator sep = new Separator();
        HBox bottomControls = new HBox();
        bottomControls.setAlignment(Pos.BOTTOM_RIGHT);
        VBox.setMargin(bottomControls, new Insets(10.0d));

        Button btnClose = new Button("Close");
        bottomControls.getChildren().add(btnClose);

        vBox.getChildren().addAll(
                topControls,
                tblCustomer,
                sep,
                bottomControls
        );

        //add Scene
        Scene scene = new Scene(vBox);
        stage.setScene(scene);
        stage.setWidth(800);
        stage.setHeight(600);
        stage.setTitle("VBox and HBox App");
        stage.setOnShown((evt)->loadTable(tblCustomer));
        stage.show();
    }

    private void loadTable(TableView<Customer> tblCustomer) {
       tblCustomer.getItems().add(new Customer("Fergo", "WashingTon"));
       tblCustomer.getItems().add(new Customer("laxliu", "Wizard"));
       tblCustomer.getItems().add(new Customer("Thomas", "Jefferson"));
    }
    public static void main(String[] args) {
        launch();
    }
}

